package com.sgb.goods.view.activity.inquiry;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.entity.inquiry.SupplierEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DisplayUtil;
import com.swgk.core.util.MToast;

/**
 * 描述：发布询价-供应商适配器
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.12.14
 */
public class FindInquirySupplierAdapter extends BaseRecycleAdapter<SupplierEntity> {

    private int selectCount = 0;
    private static final int MAX_COUNT = 10;
    private Drawable drawableVip;
    private int drawablePadding;
    private OnSupplierItemClickListener mSupplierClickListener;

    public FindInquirySupplierAdapter(Context context) {
        super(context, R.layout.goods_item_find_inquiry_supplier, null);
        initVipDrawable();
    }

    private void initVipDrawable() {
        this.drawableVip = ContextCompat.getDrawable(context, R.mipmap.goods_icon_vip_supplier);
        assert this.drawableVip != null;
        this.drawableVip.setBounds(0, 0, DisplayUtil.convertDpToPixel(context, 18), DisplayUtil.convertDpToPixel(context, 15));
        this.drawablePadding = DisplayUtil.convertDpToPixel(context, 5);
    }

    @Override
    public void convert(ItemViewHolder holder, SupplierEntity supplierEntity) {
        holder.setBinding(BR.entity, supplierEntity);
        TextView tvCompany = holder.itemView.findViewById(R.id.companyName);
        showCompanyVipIcon(tvCompany, supplierEntity);
        ImageView itemSelectOption = holder.itemView.findViewById(R.id.itemSelectOption);
        setItemImageResource(itemSelectOption, supplierEntity.isSelected());
        LinearLayout supplierItemView = holder.itemView.findViewById(R.id.ll_supplierItem);
        supplierItemView.setOnClickListener(v -> {
            if (this.selectCount >= MAX_COUNT) {
                MToast.showToast(context, "最多添加50家供应商");
                return;
            }
            supplierEntity.setSelected(!supplierEntity.isSelected());
            setItemImageResource(itemSelectOption, supplierEntity.isSelected());
            mSupplierClickListener.onClickSupplierItem(supplierEntity);
        });
    }

    /**
     * 当前被选中的供应商个数
     *
     * @param selectCount selectCount
     */
    public void setSelectCount(int selectCount) {
        this.selectCount = selectCount;
    }

    /**
     * 设置单个ITEM的状态
     *
     * @param imageView  imageView
     * @param isSelected isSelected
     */
    private void setItemImageResource(ImageView imageView, boolean isSelected) {
        if (isSelected) {
            imageView.setImageResource(R.mipmap.goods_icon_inquiry_supplier_check_on);
        } else {
            imageView.setImageResource(R.mipmap.goods_icon_inquiry_supplier_check_off);
        }
    }

    private void showCompanyVipIcon(TextView tvCompany, SupplierEntity entity) {
        if ("1".equals(entity.getFlag())) {
            tvCompany.setCompoundDrawables(null, null, drawableVip, null);
            tvCompany.setCompoundDrawablePadding(drawablePadding);
        } else {
            tvCompany.setCompoundDrawables(null, null, null, null);
        }
    }

    /**
     * 删除的回调
     */
    public interface OnSupplierItemClickListener {
        /***
         *单个供应商item的点击事件
         * @param supplierEntity supplierEntity
         */
        void onClickSupplierItem(SupplierEntity supplierEntity);
    }

    /**
     * @param listener listener
     */
    public void setOnItemClickListener(OnSupplierItemClickListener listener) {
        mSupplierClickListener = listener;
    }
}
